<template>
	<view>
		<!-- 地址 -->
		<view class="address" v-if="address!=null" @click="toadd">
			<view class="iconadd">
				<image style="width: 25rpx;" src="../../static/pay/address.png" mode="widthFix"></image>
			</view>
			<view class="info">
				<view>
					<text>{{address.name}}</text>
					<text style="margin-left: 40rpx;">{{address.phone}}</text>
				</view>
				<view class="add_detail">{{address.provinceName}}{{address.cityName}}{{address.regionName}}{{address.detail}}</view>
			</view>
			<view>
				<image style="width: 18rpx;" src="../../static/pay/icon_right.png" mode="widthFix"></image>
			</view>
		</view>
		<view class="xzadd" @click="toadd" v-else>
			点击选择地址
		</view>
		
		<!-- 商品信息 -->
		<view class="product_box" v-if="iscart==false">
			<view>
				<image style="width: 161rpx; height: 161rpx;" :src="dataInfo.imageUrl" mode="aspectFill"></image>
			</view>
			<view class="product_details">
				<view class="product_name">{{dataInfo.goodsName}}</view>
				<view class="product_specs">{{dataInfo.specSkuName}}</view>
				<view class="product_price" v-if="type==1">￥{{dataInfo.goodsPrice}}*{{num}}</view>
				<view class="product_price" v-if="type==2||type==6">{{dataInfo.goodsPrice}}积分*{{num}}</view>
			</view>
		</view>
		<view class="product_box" v-if="iscart==true" v-for="item in dataList">
			<view>
				<image style="width: 161rpx; height: 161rpx;" :src="item.goodsInfo.imageUrl" mode="aspectFill"></image>
			</view>
			<view class="product_details">
				<view class="product_name">{{item.goodsInfo.goodsName}}</view>
				<view class="product_specs">{{item.goodsInfo.specSkuName}}</view>
				<view class="product_price">{{item.goodsInfo.goodsPrice}}积分*{{item.buyNum}}</view>
			</view>
		</view>
		<!-- 规格 -->
		<view class="guige_box" v-if="type!=2&&type!=6">
			<view class="guige">
				<view class="text">商品金额</view>
				<view class="number" v-if="type!=4">￥{{dataInfo.goodsPrice}}</view>
				<view class="number" v-if="type==4">￥{{dataInfo.sharingPrice}}</view>
			</view>
			<view class="guige">
				<view class="text">赠送积分</view>
				<view class="number">{{dataInfo.givePoint}}</view>
			</view>
			<view class="guige">
				<view class="text">运费</view>
				<view class="number">￥0.00</view>
			</view>
			<view class="guige liu">
				<view class="text">备注:</view>
				<input type="text" v-model="liuyan" placeholder="请填写备注信息" class="input"/>
			</view>
		</view>
		<view class="guige_box" v-if="type==2||type==6">
			<view class="guige">
				<view class="text">支付方式</view>
				<view class="number">积分支付</view>
			</view>
			<!-- <view class="guige">
				<view class="text">赠送积分</view>
				<view class="number">{{dataInfo.givePoint}}</view>
			</view> -->
			<view class="guige">
				<view class="text">运费</view>
				<view class="number">￥0.00</view>
			</view>
			<view class="guige liu">
				<view class="text">备注:</view>
				 <input type="text" v-model="liuyan" placeholder="请填写备注信息" class="input"/>
			</view>
		</view>
		<!-- 底部 -->
		<view class="bottom">
			<view class="payment_bottom" v-if="iscart">
				<text class="shifu">实付</text>
				<text class="shifu shifu_price" >{{price}}积分</text>
			</view>
			<view class="payment_bottom" v-else>
				<text class="shifu">实付</text>
				<text class="shifu shifu_price" v-if="type==1||type==3">￥{{dataInfo.goodsPrice*num}}</text>
				<text class="shifu shifu_price" v-if="type==4 || type==5">￥{{dataInfo.sharingPrice*num}}</text>
				<text class="shifu shifu_price" v-if="type==2||type==6">{{dataInfo.goodsPrice*num}}积分</text>
			</view>
			<view class="submit" @click="ok">提交订单</view>
		</view>
	</view>
</template>

<script>
	import {buy,payyuntuan,priceOrser,cartorder,cartbuy,pbuy,pbuyOrder,yxd,msOrder} from"../../api/product.js"
	export default{
		data() {
			return {
				address:null,
				dataInfo:{},
				dataList:[],
				num:0,
				liuyan:"",
				type:1 ,//1钱购买 2积分购买 3拼团单独购买 4发起拼团 5加入拼团 6秒杀
				iscart:false,
				price:0,
				ids:"",
				activeId:null
			}
		},
		onLoad(e) {
			if(e.iscart){
				this.iscart=true
				this.getCartinfo(e.ids)
				this.ids=e.ids
				this.type=e.type
			}else{
				if(e.type==1||e.type==2){
					this.getInfo(e)
				}else if(e.type==5){
					this.getGroupInfo(e)
				}else if(e.type==6){
					this.getMsInfo(e)
				}
				if(e.activeId){
					this.activeId=e.activeId
				}
				
				this.type=e.type
			}
			console.log(e)
		},
		onShow() {
			uni.$once('setAddress',(data)=>{
			    console.log(data);
				this.address=data
				uni.$off('setAddress')
			})
		},
		methods: {
			// 购物车下单
			async getCartinfo(data){
				let res=await this.get(cartorder,{shopGoodsId:data})
				this.address=res.data.userDefaultAddress
				this.dataList=res.data.goodsList
				this.dataList.forEach(item=>{
					this.price=(item.buyNum*item.goodsInfo.goodsPrice)+this.price
				})
				console.log(this.price)
			},
			//获取正常购买商品订单信息
			async getInfo(data){
				let res=await this.iget(buy,data)
				console.log(res)
				if(res.code==1){
					this.address=res.data.userDefaultAddress
					this.dataInfo=res.data.goodsInfo
					this.num=res.data.buyNum
				}else if(res.code==0){
					this.msg(res.msg,"none")
					setTimeout(()=>{
						uni.navigateBack({
							delta:1
						})
					},1500)
				}
			},
			//获取秒杀商品订单信息
			async getMsInfo(data){
				let res=await this.iget(yxd,data)
				console.log(res)
				if(res.code==1){
					this.address=res.data.userDefaultAddress
					this.dataInfo=res.data.goodsInfo
					this.num=res.data.buyNum
				}else if(res.code==0){
					this.msg(res.msg,"none")
					setTimeout(()=>{
						uni.navigateBack({
							delta:1
						})
					},1500)
				}
			},
			// 获取拼团购买商品订单信息
			async getGroupInfo(e){
				let data=JSON.parse(JSON.stringify(e))
				if(data.type==3){data.type=10}
				if(data.type==4){data.type=20}
				let res=await this.iget(pbuy,data)
				if(res.code==1){
					this.address=res.data.userDefaultAddress
					this.dataInfo=res.data.goodsInfo
					this.num=res.data.buyNum
				}else if(res.code==0){
					this.msg(res.msg,"none")
					setTimeout(()=>{
						uni.navigateBack({
							delta:1
						})
					},1500)
				}
			},
			// 去更换地址
			toadd(){
				uni.navigateTo({
					url:"/pages/address/addresslist?type=xz"
				})
			},
			// 下单
			async ok(){
				if(this.address==null){
					this.msg("收货地址未选择","none")
				}else{
					if(this.iscart){
						let res=await this.post(cartbuy,{
							orderAddressId:this.address.addressId,
							buyerRemark:this.liuyan,
							shopGoodsId:this.ids
						})
						this.msg("下单成功")
						uni.$emit('refresh',{})
						setTimeout(()=>{
							uni.navigateTo({
								url:"/pages/product/paysuccess?type=2&price="+this.price+"&code="+res.data
							})
						},1500)
					}else{
						// 这个是钱和余额购买
						if(this.type==1){
							let res=await this.post(priceOrser,{
								goodsId:this.dataInfo.goodsId,
								goodsSkuId:this.dataInfo.goodsSkuId,
								userAddressId:this.address.addressId,
								buyerRemark:this.liuyan,
								goodsNumber:this.num,
								goodsAttr:this.dataInfo.specSkuName
							})
							uni.navigateTo({
								url:"/pages/product/payorder?order="+res.data.orderNo+"&price="+res.data.totalPrice
							})
							// 积分购买
						}else if(this.type==2||this.type==6){
							let res=await this.post(payyuntuan,{
								goodsId:this.dataInfo.goodsId,
								goodsNumber:this.num,
								goodsSkuId:this.dataInfo.goodsSkuId,
								goodsAttr:this.dataInfo.specSkuName,
								orderAddressId:this.address.addressId,
								buyerRemark:this.liuyan
							})
							this.msg("下单成功")
							setTimeout(()=>{
								uni.navigateTo({
									url:"/pages/product/paysuccess?type=2&price="+this.dataInfo.goodsPrice+"&code="+res.data
								})
							},1500)
							// 拼团单独购买
						}else if(this.type==3){
							let res=await this.post(pbuyOrder,{
								goodsId:this.dataInfo.goodsId,
								goodsSkuId:this.dataInfo.goodsSkuId,
								userAddressId:this.address.addressId,
								buyerRemark:this.liuyan,
								goodsNumber:this.num,
								goodsAttr:this.dataInfo.specSkuName,
								orderType:10
							})
							uni.navigateTo({
								url:"/pages/product/payorder?order="+res.data.orderNo+"&price="+res.data.totalPrice+"&ordertype="+res.data.orderType
							})
							// 发起拼团
						}else if(this.type==4){
							let res=await this.post(pbuyOrder,{
								goodsId:this.dataInfo.goodsId,
								goodsSkuId:this.dataInfo.goodsSkuId,
								userAddressId:this.address.addressId,
								buyerRemark:this.liuyan,
								goodsNumber:this.num,
								goodsAttr:this.dataInfo.specSkuName,
								orderType:20,
								activeId:this.activeId
							})
							uni.navigateTo({
								url:"/pages/product/payorder?order="+res.data.orderNo+"&price="+res.data.totalPrice+"&ordertype="+res.data.orderType
							})
						}else if(this.type==5){
							let res=await this.post(pbuyOrder,{
								goodsId:this.dataInfo.goodsId,
								goodsSkuId:this.dataInfo.goodsSkuId,
								userAddressId:this.address.addressId,
								buyerRemark:this.liuyan,
								goodsNumber:this.num,
								goodsAttr:this.dataInfo.specSkuName,
								orderType:30,
								activeId:this.activeId
							})
							uni.navigateTo({
								url:"/pages/product/payorder?order="+res.data.orderNo+"&price="+res.data.totalPrice+"&ordertype="+res.data.orderType
							})
						}else if(this.type==6){
							// let res=await this.post(msOrder,{
							// 	goodsId:this.dataInfo.goodsId,
							// 	goodsNumber:this.num,
							// 	goodsSkuId:this.dataInfo.goodsSkuId,
							// 	goodsAttr:this.dataInfo.specSkuName,
							// 	userAddressId:this.address.addressId,
							// 	buyerRemark:this.liuyan
							// })
							// this.msg("下单成功")
							// setTimeout(()=>{
							// 	uni.navigateTo({
							// 		url:"/pages/product/paysuccess?type=2&price="+this.dataInfo.goodsPrice+"&code="+res.data
							// 	})
							// },1500)
						}
					}	
				}
				
			}
		},
	}
</script>

<style scoped="">
	.input{
		font-size: 26rpx;
		width: 500rpx;
	}
	.liu{
		justify-content: flex-start !important;
	}
	.xzadd{
		height: 100rpx;
		line-height: 100rpx;
		text-align: center;
		font-weight: bold;
		font-size: 32rpx;
		color:#F0812D ;
	}
	.address{
		width: 690rpx;
		margin: 26rpx auto;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}
	.info{
		width: 600rpx;
		font-size: 28rpx;
		font-weight: 400;
		color: #000000;
	}
	.iconadd{
		position: relative;
		top: -20rpx;
	}
	.add_detail{
		text-overflow: ellipsis;
		overflow: hidden;
		white-space: nowrap;
		margin-top: 12rpx;
	}
	.product_box{
		width: 690rpx;
		height: 161rpx;
		margin: 60rpx auto 0;
		display: flex;
		/* align-items: center; */
	}
	.product_details{
		margin-left: 30rpx;
	}
	.product_name{
		font-size: 26rpx;
		font-weight: 800;
		color: #333333;
		overflow: hidden;  
		text-overflow: ellipsis;  
		display: -webkit-box;  
		-webkit-line-clamp: 2;  
		-webkit-box-orient: vertical; 
	}
	.product_specs{
		font-size: 22rpx;
		font-weight: 400;
		color: #666666;
		margin-top: 14rpx;
	}
	.product_price{
		font-size: 32rpx;
		font-weight: bold;
		color: #F0812D;
		margin-top: 10rpx;
	}
	.guige_box{
		width: 690rpx;
		margin: 130rpx auto 0;
	}
	.guige{
		width: 690rpx;
		margin: 45rpx auto;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}
	.text{
		font-size: 26rpx;
		font-weight: 600;
		color: #666666;
	}
	.number{
		font-size: 28rpx;
		font-weight: 800;
		color: #000000;
	}
	.bottom{
		width: 750rpx;
		height: 88rpx;
		position: fixed;
		bottom: 0;
		left: 0;
		background: #F6F6F6;
		line-height: 88rpx;
		display: flex;
	}
	.payment_bottom{
		width: 507rpx;
		padding-left: 30rpx;
	}
	.shifu{
		font-size: 28rpx;
		font-weight: bold;
		color: #000000;
	}
	.shifu_price{
		margin-left: 10rpx;
	}
	.submit{
		width: 243rpx;
		height: 88rpx;
		background: linear-gradient(90deg, #F3C44E 0%, #F18D29 53%, #ED6138 100%);
		z-index: 3;
		font-size: 32rpx;
		font-weight: 400;
		line-height: 88rpx;
		color: #FFFFFF;
		text-align: center;
	}
</style>
